<?php

namespace App\Commands;

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;

class Go extends Command
{
    /**
     * The signature of the command.
     *
     * @var string
     */
    protected $signature = 'go';

    /**
     * The description of the command.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $cookies = [
            'PHPSESSID' => 'a7qvgbbio0ka89bta0bg6d00r3',
        ];
        $ids       = ['807', '808', '809', '810'];
        $keys      = 'uQ7eDBxV';
        $base_url  = 'http://weixin.iinnc.com/';
        $jar       = new CookieJar();
        $domain    = 'weixin.iinnc.com';
        $cookieJar = $jar->fromArray($cookies, $domain);
        $client    = new Client([
            'timeout' => '20',
            'headers' => [
                'user-agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.901.400 QQBrowser/9.0.2524.400',
            ],
        ]);

        foreach ($ids as $id) {
            // 获取 hash
            $response = $client->request('GET', $base_url . '?keys=' . $keys . '&gu&act=fav', [
                'cookies' => $cookieJar,
            ]);
            $content = $response->getBody()->getContents();
            preg_match_all("/__hash__ = '(.*?)',baseurl/", $content, $hash);
            $hash_str = $hash[1][0];
            // 提交数据
            $response = $client->request('POST', $base_url . '?keys=' . $keys . '&gu&do=order', [
                'cookies'     => $cookieJar,
                'form_params' => [
                    'do'       => 'order',
                    'id'       => $id,
                    '__hash__' => $hash_str,
                ],
            ]);
            $content = $response->getBody()->getContents();
            $res     = json_decode($content, true);
            if ($res['data']['status'] == 'success') {
                $this->info($id . ' Done.');
                break;
            }
        }

        $this->info('OK.');

        // 提交数据
        // $response = $client->request('POST', $base_url . '?keys=' . $keys . '&gu&do=fav', [
        //     'cookies'     => $cookieJar,
        //     'form_params' => [
        //         'do'       => 'fav',
        //         'id'       => '987',
        //         '__hash__' => $hash_str,
        //     ],
        // ]);
        // $content = $response->getBody()->getContents();
        // $res     = json_decode($content, true);
        // if ($res['data']['status'] == 'success') {
        // }
    }

    /**
     * Define the command's schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    public function schedule(Schedule $schedule): void
    {
        // $schedule->command(static::class)->everyMinute();
    }
}
